<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
<title>title</title>
<link rel="stylesheet" href="../assets/agile/css/ratchet/css/ratchet.min.css">
<link rel="stylesheet" href="../assets/agile/css/flat/flat.component.css">
<link rel="stylesheet" href="../assets/agile/css/flat/flat.color.css">
<link rel="stylesheet" href="../assets/agile/css/flat/iconline.css">
<link rel="stylesheet" href="../assets/app/css/app.css" />
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery.js"></script>
<script type="text/javascript" src="../script/config.js"></script>
<script type="text/javascript" src="../script/jquery.form.js"></script>
<script type="text/javascript" src="../script/local.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
</head>
<body>
<div class="tag_history box_shadow" style="display:none;top:103px;">
<!-- item="none" -->
	<div class="history_main" item="content"></div>
</div>
<div class="mask"></div>
<div id="ios-top-box">
    <div id="header" class="bar bar-nav">
    <div class="titlebar">
    <a href="javascript:appClose('search',api);"><i class="iconfont iconline-arrow-left"></i></a>
    <h1 class="title">搜索结果</h1>
    </div>
    </div>
</div>
<header class="bar bar-nav box_shadow head_a history-head" style="margin-top:44px;display:none;height:40px !important;z-index:109;">
	<a href="javascript:;" style="text-align:left;font-size:13px;">历史记录</a>
	<button class="common-btn clear-history" style="margin-top:8px;">清除历史记录</button>
</header>

<div style="margin-top:64px;">
	<div class="scroller create_goods" style="max-width: 640px;margin:0 auto;">
			<p style="text-align:center;padding-top:30px;">暂无搜索结果</p>	

</div>
</div>

<script type="text/javascript">
/*
GetJPData(config.url+'/app.php?act=search',{},function(data){
	if(data['status']){
		$('.history_main').html(data['html']);
	}
});
*/
apiready = function(){
	var keywords = localData.get('search_keywords');
	ajaxSearch(keywords);
	history_tag();
	function history_tag(){
		var history_tag = localData.get('search_keywords');
		if (history_tag) {
			var history_obj = $.parseJSON(history_tag);
			if (typeof history_obj == 'object') {
				var arr = new Array();
				var k = 0;
				for (var j in history_obj) {
					arr[k] = history_obj[j];
					k++;
				}
				var html = '';
				for (var i = arr.length - 1; i >= 0; i--) {
					html += '<a href="javascript:search_tag(\'' + arr[i] + '\');" item="' + i + '" class="click-history-search">' + arr[i] + '</a>';
				}
				$('.history_main').html(html);
			}
		}
	}
	
	//焦点搜索输入框,显示搜索记录标签
	/*
	 var keyword_box = $('input[name=keywords]');
	 var tag_history = $('.tag_history');
	 keyword_box.click(function(){
	 history_tag();
	 if($('.history_main').attr('item') == 'content'){
	 if (tag_history.attr('noshow') == undefined) {
	 setTimeout(function(){
	 $('.history-head').fadeIn();
	 tag_history.fadeIn();
	 $('.mask').show();
	 }, 100);
	 }
	 }
	 });
	 */
	//清除历史记录
	$('.clear-history').click(function(){
		/*
		 $.ajax({
		 url : config.url+'/app.php?act=clearhistory',
		 success : function(res){
		 if(res == 1){
		 $('.history_main').html('');
		 }
		 },
		 });
		 */
		localData.remove('search_keywords');
		localData.remove('search_time');
		$('.history_main').html('');
		_history(tag_history);
		$('.mask').hide();
	});
	
	function _history(tag_history){
		tag_history.fadeOut();
		$('.history-head').fadeOut();
		$('#flat_article').css('top', '45px');
	}
	
	/**
	 * $('body').on('click','.click-history-search',function(){
	 var keyword = $(this).attr('item');
	 tag_history.fadeOut();
	 $('.history-head').fadeOut();
	 $('.mask').fadeOut();
	 ajaxSearch(keyword);
	 });
	 */
	function search_tag(keywords){
		tag_history.fadeOut();
		$('.history-head').fadeOut();
		$('.mask').fadeOut();
		ajaxSearch(keywords);
	}
	
	function ajaxSearch(keywords){
		$('input[name=keywords]').val(keywords);
		$.ajax({
			type: 'post',
			url: config.url + '/app.php?act=search',
			data: {
				'search': true,
				'keywords': keywords
			},
			beforeSend: function(xhr){
				//$.PageDialog.fail('正在搜索');
				api.showProgress({
					style: 'default',
					animationType: 'fade',
					title: '努力加载中...',
					text: '先喝杯茶...',
					modal: false
				});
			},
			success: function(res, status, xhr){
				api.hideProgress();
				var html = '';
				if (res['data'] == null) {
					$.PageDialog.fail('没有搜索相关商品');
				}
				else 
					if (res['data'] != null) {
						$('.create_goods').html('');
						for (var i = 0; i < res['data'].length; i++) {
							html += '<ul class="search-ul"><li>';
							html += '<div class="search_goods_img"><a href="javascript:openItem(\'item\',' + res['data'][i]['id'] + ',function(id){localData.set(\'goods_id\',id);});"><img class="lazy" data-original="' + config.upload_url + '/' + res['data'][i]['thumb'] + '" /></a></div>';
							html += '<div class="search_goods_info">';
							html += '<p class="title_shopname"><a href="javascript:openItem(\'item\',' + res['data'][i]['id'] + ',function(id){localData.set(\'goods_id\',id);});">' + res['data'][i]['title'] + '</a></p>';
							html += '<div class="goods_info_bottom"><div class="goods_con"><progress max="100" value="' + parseInt((res['data'][i]['canyurenshu'] / res['data'][i]['zongrenshu']) * 100) + '" class="css3">';
							html += '<div class="progress-bar"></div></progress>';
							html += '<p><span class="left">总需：' + res['data'][i]['zongrenshu'] + '</span><span class="right">剩余：' + res['data'][i]['shenyurenshu'] + '</span></p>';
							html += '</div><div class="btn_buy addCart" item="' + res['data'][i]['id'] + '" onclick="addCart(' + res['data'][i]['id'] + ',' + res['data'][i]['yunjiage'] + ',1);">购买</div>';
							html += '</div></div></li></ul>';
						}
						$('.create_goods').html(html);
						/*
					 if(res['html']){
					 $('.history_main').html(res['html']);
					 }
					 */
						lyzimg();
					}
			}
		});
	}
	
	//点击搜索商品
	$('.click-search').on('click', function(){
		var keywords = $('input[name=keywords]').val();
		if (keywords == '') {
			$.PageDialog.fail('请输入商品关键词');
			return;
		}
		var now_date = parseInt(new Date().getTime() / 1000);
		var search_time = localData.get('search_time');
		if (search_time) {
			var diff = now_date - parseInt(search_time);
			if (diff >= 3600 * 24 * 30) {
				localData.remove('search_keywords');
				localData.remove('search_time');
			}
		}
		else {
			localData.set('search_time', now_date);
		}
		
		var search_keywords = localData.get('search_keywords');
		var isEmpty = false;
		if (!search_keywords) {
			search_keywords = {};
			isEmpty = true;
		}
		else {
			search_keywords = $.parseJSON(search_keywords);
			if (typeof search_keywords != 'object') {
				search_keywords = {};
				isEmpty = true;
			}
		}
		
		if (!isEmpty) {
			for (var i in search_keywords) {
				if (search_keywords[i] == keywords) {
					delete search_keywords[i];
				}
			}
		}
		search_keywords[now_date] = keywords;
		localData.set('search_keywords', JSON.stringify(search_keywords));
		tag_history.fadeOut();
		$('.history-head').fadeOut();
		$('.mask').fadeOut();
		ajaxSearch(keywords);
		return false;
	});
	//搜索时遮罩
	$('.mask').bind('touchstart', function(){
		$(this).hide();
		_history(tag_history);
	});
}
</script>
</body>
</html>